package com.henrybit.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.henrybit.entity.HotelInfo;
import com.henrybit.entity.LandDetailInfo;
import com.henrybit.exception.DBException;
import com.henrybit.util.Constant;
import com.henrybit.util.DBFactory;

/**
 * 岛屿详细信息Dao<br>
 * @author henrybit
 * @version 1.0
 */
public class LandDetailInfoDao extends BaseDao<LandDetailInfo> {
	
	/**查询基础SQL*/
	private static String queryPre = "select * from land_detail_info where 1=1";
	
	@Override
	public List<LandDetailInfo> query(LandDetailInfo t) throws DBException {
		// TODO Auto-generated method stub
		List<LandDetailInfo> list = new ArrayList<LandDetailInfo>();
		String sql = prepareQuerySql(t);
		Connection con = DBFactory.getConnection();
		PreparedStatement ps = null;
		try {
			con.setAutoCommit(false);
			ps = con.prepareStatement(sql);
			ResultSet rs = ps.getResultSet();
			while(rs.next()) {
				LandDetailInfo landDetailInfo = getLandDetailInfo(rs);
				list.add(landDetailInfo);
			}
			rs.close();
			ps.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new DBException(e, Constant.DB_QUERY_LAND_DETAIL_INFO_ERROR, "准备岛屿详情查询SQL执行异常");
		} finally {
			try {
				con.close();
			} catch(SQLException e) {
				throw new DBException(e, Constant.DB_CONNECTION_CLOSE_ERROR, "关闭数据库连接异常");
			}
		}
		return list;
	}
	
	

	@Override
	public void insert(LandDetailInfo t) throws DBException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean update(LandDetailInfo t) throws DBException {
		// TODO Auto-generated method stub
		return false;
	}
	
	private String prepareQuerySql(LandDetailInfo t) {
		int id = t.getId();
		String name = t.getName();
		StringBuilder sql = new StringBuilder(queryPre);
		if(id > 0) {
			sql.append(" and id=").append(id);
		}
		if(name != null && !name.equals("")) {
			sql.append(" and name like '%").append(name).append("%'");
		}
		return sql.toString();
	}
	
	/**
	 * 封装岛屿详情信息<p>
	 * @param rs
	 * @return
	 * @throws DBException
	 */
	private LandDetailInfo getLandDetailInfo(ResultSet rs) throws DBException {
		LandDetailInfo landDetailInfo = new LandDetailInfo();
		try {
			int id = rs.getInt("id");
			int landId = rs.getInt("landId");
			String name = rs.getString("name");
			String description = rs.getString("description");
			landDetailInfo.setId(id);
			landDetailInfo.setLandId(landId);
			landDetailInfo.setName(name);
			landDetailInfo.setDescription(description);
		} catch (Exception e) {
			throw new DBException(e, Constant.DB_HOTELINFO_PREPARE_DATA_ERROR, "封装岛屿详情信息类异常");
		}
		return landDetailInfo;
	}

}
